/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.data;

import static com.vng.zing.survey.data.Base.deserialize;
import static com.vng.zing.survey.data.Base.serialize;
import com.vng.zing.survey.thrift.model.Action;
import com.vng.zing.survey.thrift.model.Error;
import com.vng.zing.survey.thrift.model.IDUserResQuesNoRequiKey;
import com.vng.zing.survey.thrift.model.IDUserResponseItem;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import kyotocabinet.DB;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class IDUserResponseNoRequiData extends Base{
    private static final Logger logger_ = Logger.getLogger(IDUserResponseData.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static IDUserResponseNoRequiData _instance = null;
    private final DB db;

    public IDUserResponseNoRequiData() {
        db = new DB();
        if (!db.open("database/iduserresponsenorequire.kch"+cofigid , DB.OWRITER | DB.OCREATE | DB.OAUTOTRAN)) {
            System.err.println("open error: " + db.error());
        }
    }

    public static IDUserResponseNoRequiData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new IDUserResponseNoRequiData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }
     public IDUserResponseItem getIDUserResQuesNoRequi(int idSurvey, short idQues) {
        try {
            IDUserResQuesNoRequiKey dUserResQuseNoReQuriKey = new IDUserResQuesNoRequiKey(idSurvey, idQues);
            byte[] key = serialize(dUserResQuseNoReQuriKey);
            byte[] value = db.get(key);
            if (value == null) {
                return null;
            }
            IDUserResponseItem dUserResponseItem = new IDUserResponseItem();
            dUserResponseItem = (IDUserResponseItem) deserialize(value);
            return dUserResponseItem;

        } catch (IOException ex) {
            logger_.error("IDUserResponseData.getIDUserResQuesNoRequi:" + ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Error setIDUserResQuesNoRequi(int idSurvey, short idQues, IDUserResponseItem dUserResponseItem) {
        try {
            IDUserResQuesNoRequiKey dUserResQuseNoReQuriKey = new IDUserResQuesNoRequiKey(idSurvey, idQues);
            byte[] key = serialize(dUserResQuseNoReQuriKey);
            byte[] value = serialize(dUserResponseItem);
            if (!db.set(key, value)) {

                return Error.ERR_SYSTEM;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
            logger_.error("IDUserResponseData.setIDUserResQuesNoRequi:" + ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }

    public Error removeIDUserResQuesNoRequi(int idSurvey, short idQues) {

        try {
            IDUserResQuesNoRequiKey dUserResQuseNoReQuriKey = new IDUserResQuesNoRequiKey(idSurvey, idQues);
            byte[] key = serialize(dUserResQuseNoReQuriKey);
            if (!db.remove(key)) {
                return Error.ERR_SYSTEM;

            }
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Error.SUCCESS;

    }

    public Error updateIDUserResQuesNoRequi(int idSurvey, short idQues, int idUser, Action action) {

        db.begin_transaction(false);
        try {
            IDUserResQuesNoRequiKey dUserResQuseNoReQuriKey = new IDUserResQuesNoRequiKey(idSurvey, idQues);
            IDUserResponseItem dUserResponseItem = new IDUserResponseItem(new ArrayList<Integer>(100000));
            byte[] key = serialize(dUserResQuseNoReQuriKey);
            byte[] value = db.get(key);
            if (value != null) {
                dUserResponseItem = (IDUserResponseItem) deserialize(value);
            }
            if (action == Action.ADD) {
                dUserResponseItem.getIdUsers().add(0, idUser);
            } else if (action == Action.REMOVE) {
                dUserResponseItem.getIdUsers().remove((Integer) idUser);

            }
            value = serialize(dUserResponseItem);
            if (!db.set(key, value)) {
                db.end_transaction(true);
                return Error.ERR_SYSTEM;
            }

        } catch (IOException ex) {
            //  logger_.error("IDUserResponseData.updateIDUserResQuesNoRequi:" + ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.end_transaction(true);
        return Error.SUCCESS;

    }

}
